﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MangaManagerLib;

namespace MangaManagerLib
{
    public static class ConvertFunctionHelper
    {
        public static BriefMangaInfoContract ConvertToBriefMangaStoryContract(this MangaStory manga)
        {
            BriefMangaInfoContract mangaStoryContract = new BriefMangaInfoContract();
            mangaStoryContract.Link = manga.Link;
            mangaStoryContract.Title = manga.Title;
            mangaStoryContract.Status = manga.Status;
            mangaStoryContract.ImageLink = manga.ImageLink;
            if (manga.ChapterNewest != null)
            {
                mangaStoryContract.ChapterNewest = new Chapter() { Date = manga.ChapterNewest.Date, Link = manga.ChapterNewest.Link, Title = manga.ChapterNewest.Title };
            }
            return mangaStoryContract;
        }
        public static IList<BriefMangaInfoContract> ConvertToListBriefMangaStoryContract(this IList<MangaStory> listManga)
        {
            IList<BriefMangaInfoContract> result = new List<BriefMangaInfoContract>();
            foreach (MangaStory mangaStory in listManga)
            {
                result.Add(mangaStory.ConvertToBriefMangaStoryContract());
            }
            return result;
        }
        public static MangaStoryDetailContract ConvertToMangaDetailContract(this MangaStoryDetail mangaStoryDetail)
        {
            if (mangaStoryDetail == null)
                return null;
            MangaStoryDetailContract mangaDetailContract = new MangaStoryDetailContract() { Author = mangaStoryDetail.Author, 
                Chapters = mangaStoryDetail.Chapters.ConvertListChapterToListMangaChapter(), ImageLink= mangaStoryDetail.ImageLink,
                Link = mangaStoryDetail.Link, Summary = mangaStoryDetail.Summary, Title = mangaStoryDetail.Title, Genres = mangaStoryDetail.Genres,
                OtherName = mangaStoryDetail.OtherName, Status = mangaStoryDetail.Status, Views = mangaStoryDetail.Views, ChapterNewest = mangaStoryDetail.ChapterNewest};
            return mangaDetailContract;
        }
        public static MangaChapter ConvertChapterToMangaChapter(this Chapter chapter)
        {
            if (chapter == null)
                return null;
            MangaChapter resultValue = new MangaChapter() { Link = chapter.Link, Title = chapter.Title, Date = chapter.Date};
            return resultValue;
        }
        public static IList<MangaChapter> ConvertListChapterToListMangaChapter(this IList<Chapter> list)
        {
            if (list == null || list.Count < 1)
                return null;
            IList<MangaChapter> listMangaChapter = new List<MangaChapter>();
            foreach (Chapter c in list)
            {
                listMangaChapter.Add(c.ConvertChapterToMangaChapter());
            }
            return listMangaChapter;
        }
        public static MangaCollectionContract ConvertToMangaCollectionContract(this MangaCollection mangaCollection)
        {
            if (mangaCollection == null)
                return null;
            MangaCollectionContract mangaCollectionContract = new MangaCollectionContract() { CurrentPage = mangaCollection.CurrentPage, TotalPage = mangaCollection.TotalPage};
            mangaCollectionContract.MangaList = new List<MangaStoryContract>();
            foreach(MangaStory m in mangaCollection.MangaList)
            {
                MangaStoryContract storyContract = m.ConvertToMangaStoryContract();
                mangaCollectionContract.MangaList.Add(storyContract);
            }
            return mangaCollectionContract;
        }
        public static MangaStoryContract ConvertToMangaStoryContract(this MangaStory manga)
        {
            MangaStoryContract mangaStoryContract = new MangaStoryContract();
            mangaStoryContract.Link = manga.Link;
            mangaStoryContract.Summary = manga.Summary;
            mangaStoryContract.ImageLink = manga.ImageLink;
            mangaStoryContract.Title = manga.Title;
            mangaStoryContract.Author = manga.Author;
            mangaStoryContract.Genres = manga.Genres;
            mangaStoryContract.OtherName = manga.OtherName;
            mangaStoryContract.Status = manga.Status;
            mangaStoryContract.Views = manga.Views;
            if(manga.ChapterNewest!=null)
            {
                mangaStoryContract.ChapterNewest = new Chapter() { Date = manga.ChapterNewest.Date, Link = manga.ChapterNewest.Link, Title = manga.ChapterNewest.Title };
            }
            return mangaStoryContract;
        }
        public static IList<MangaStoryContract> ConvertToListMangaStoryContract(this IList<MangaStory> listManga)
        {
            IList<MangaStoryContract> result = new List<MangaStoryContract>();
            foreach (MangaStory mangaStory in listManga)
            {
                result.Add(mangaStory.ConvertToMangaStoryContract());
            }
            return result;
        }
    }
}